#!/usr/bin/env bash

# PACKAGE: Gunther
# VERSION: 2.0
# DATE: 10/09/08
# AUTHOR(S): Zarrar Shehzad

# COMMANDS
declare -a COMMANDS
declare -a COMMANDS_INFO
declare -a COMMANDS_INPUT
declare -a COMMANDS_OUTPUT

# Initial Input and final output
BIG_INPUT="${brain} ${gFN_STANDARD} ${funcExample}"
BIG_OUTPUT="${regDir}/example_func2standard.${gOUTPUT_TYPE}"

## 1. SETUP
COMMANDS[1]="cd `dirname ${regDir}`"
COMMANDS[2]="mkdir ${regDir} 2> /dev/null; echo -n ''"
COMMANDS[3]="cd ${regDir}"
### copy anatomical
COMMANDS[4]="fslmaths ${brain} highres"
### copy standard
COMMANDS[5]="fslmaths ${gFN_STANDARD} standard"
### copy functional (only 1 time point)
COMMANDS[6]="fslmaths ${funcExample} example_func"


# 2. REGISTER FUNC->T1
# Register functional image to subject's anatomical
# special conditions: 
#	- uses normalized mutal information as cost function
#	- rigid-body (7 degrees of freedom)
COMMANDS[7]="flirt -ref highres -in example_func -out example_func2highres -omat example_func2highres.mat -cost ${cost_func2highres} -searchcost ${cost_func2highres} -dof 7 -searchrx -90 90 -searchry -90 90 -searchrz -90 90 -interp trilinear"
# Create mat file for conversion from subject's anatomical to functional
COMMANDS[8]="convert_xfm -inverse -omat highres2example_func.mat example_func2highres.mat"
# Run vario#us commands to get pretty images
COMMANDS[9]="slicer example_func2highres highres -s 2 -x 0.35 sla.png -x 0.45 slb.png -x 0.55 slc.png -x 0.65 sld.png -y 0.35 sle.png -y 0.45 slf.png -y 0.55 slg.png -y 0.65 slh.png -z 0.35 sli.png -z 0.45 slj.png -z 0.55 slk.png -z 0.65 sll.png"
COMMANDS[10]="pngappend sla.png + slb.png + slc.png + sld.png + sle.png + slf.png + slg.png + slh.png + sli.png + slj.png + slk.png + sll.png example_func2highres1.png"
COMMANDS[11]="slicer highres example_func2highres -s 2 -x 0.35 sla.png -x 0.45 slb.png -x 0.55 slc.png -x 0.65 sld.png -y 0.35 sle.png -y 0.45 slf.png -y 0.55 slg.png -y 0.65 slh.png -z 0.35 sli.png -z 0.45 slj.png -z 0.55 slk.png -z 0.65 sll.png"
COMMANDS[12]="pngappend sla.png + slb.png + slc.png + sld.png + sle.png + slf.png + slg.png + slh.png + sli.png + slj.png + slk.png + sll.png example_func2highres2.png"
COMMANDS[13]="pngappend example_func2highres1.png - example_func2highres2.png example_func2highres.png"
COMMANDS[14]="rm -f sl?.png example_func2highres1.png example_func2highres2.png"

# 3. REGISTER T1->STANDARD
## Linear registration
COMMANDS[15]="flirt -ref standard -in highres -out highres2standard -omat highres2standard.mat -cost ${cost_highres2standard} -searchcost ${cost_highres2standard} -dof 12 -searchrx -90 90 -searchry -90 90 -searchrz -90 90 -interp trilinear"
## Create mat file for conversion from standard to high res
COMMANDS[16]="convert_xfm -inverse -omat standard2highres.mat highres2standard.mat"
## make pretty pictures
COMMANDS[17]="slicer highres2standard standard -s 2 -x 0.35 sla.png -x 0.45 slb.png -x 0.55 slc.png -x 0.65 sld.png -y 0.35 sle.png -y 0.45 slf.png -y 0.55 slg.png -y 0.65 slh.png -z 0.35 sli.png -z 0.45 slj.png -z 0.55 slk.png -z 0.65 sll.png"
COMMANDS[18]="pngappend sla.png + slb.png + slc.png + sld.png + sle.png + slf.png + slg.png + slh.png + sli.png + slj.png + slk.png + sll.png highres2standard1.png"
COMMANDS[19]="slicer standard highres2standard -s 2 -x 0.35 sla.png -x 0.45 slb.png -x 0.55 slc.png -x 0.65 sld.png -y 0.35 sle.png -y 0.45 slf.png -y 0.55 slg.png -y 0.65 slh.png -z 0.35 sli.png -z 0.45 slj.png -z 0.55 slk.png -z 0.65 sll.png"
COMMANDS[20]="pngappend sla.png + slb.png + slc.png + sld.png + sle.png + slf.png + slg.png + slh.png + sli.png + slj.png + slk.png + sll.png highres2standard2.png"
COMMANDS[21]="pngappend highres2standard1.png - highres2standard2.png highres2standard.png"
COMMANDS[22]="rm -f sl?.png highres2standard1.png highres2standard2.png"

# 4. REGISTER FUNC->STANDARD
## Create mat file for registration of functional to standard
COMMANDS[23]="convert_xfm -omat example_func2standard.mat -concat highres2standard.mat example_func2highres.mat"
## do registration
COMMANDS[24]="flirt -ref standard -in example_func -out example_func2standard -applyxfm -init example_func2standard.mat -interp trilinear"
## Create mat file for registration of standard to functional
COMMANDS[25]="convert_xfm -inverse -omat standard2example_func.mat example_func2standard.mat"
## Create pretty pictures
COMMANDS[26]="slicer example_func2standard standard -s 2 -x 0.35 sla.png -x 0.45 slb.png -x 0.55 slc.png -x 0.65 sld.png -y 0.35 sle.png -y 0.45 slf.png -y 0.55 slg.png -y 0.65 slh.png -z 0.35 sli.png -z 0.45 slj.png -z 0.55 slk.png -z 0.65 sll.png"
COMMANDS[27]="pngappend sla.png + slb.png + slc.png + sld.png + sle.png + slf.png + slg.png + slh.png + sli.png + slj.png + slk.png + sll.png example_func2standard1.png"
COMMANDS[28]="slicer standard example_func2standard -s 2 -x 0.35 sla.png -x 0.45 slb.png -x 0.55 slc.png -x 0.65 sld.png -y 0.35 sle.png -y 0.45 slf.png -y 0.55 slg.png -y 0.65 slh.png -z 0.35 sli.png -z 0.45 slj.png -z 0.55 slk.png -z 0.65 sll.png"
COMMANDS[29]="pngappend sla.png + slb.png + slc.png + sld.png + sle.png + slf.png + slg.png + slh.png + sli.png + slj.png + slk.png + sll.png example_func2standard2.png"
COMMANDS[30]="pngappend example_func2standard1.png - example_func2standard2.png example_func2standard.png"
COMMANDS[31]="rm -f sl?.png example_func2standard1.png example_func2standard2.png"

# 5. Copy things to check into the checks folder
COMMANDS_INFO[32]="Copying images into ${checksDir}/2_reg for user to CHECK DATA"
COMMANDS[32]="mkdir -p ${checksDir}/2_reg 2> /dev/null; echo -n ''"
COMMANDS[33]="cp *.png ${checksDir}/2_reg"
